﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using tcpAsyncPacket;
namespace server
{
    public partial class txserver : Form
    {
        private tcpAsyncServer server;
        public txserver()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text=="开启")
            {
                server = new tcpAsyncServer(int.Parse(textBox1.Text));
                server.clientIn += Server_clientIn;
                server.clientOut += Server_clientOut;
                server.recData += Server_recData;
                server.unexpectedStop += Server_unexpectedStop;
                server.errorRec += Server_errorRec;
                server.start();
                button1.Text = "停止";
            }
            else
            {
                server.stop();
                button1.Text = "开启";
            }


        }

        private void Server_errorRec(object sender, infoArg e)
        {
            this.BeginInvoke(new Action(() => listBox3.Items.Add(e.message)));
        }

        private void Server_unexpectedStop(object sender, infoArg e)
        {
            server.stop();
            button1.Text = "开启";
            MessageBox.Show("wrong:" + e.iType.ToString() + ":" + e.message);
        }

        private void voke(Action inact)
        {
            this.BeginInvoke(inact);
        }

        private int ct = 0;
        private void Server_recData(object sender, netArg e)
        {
            if (e.isFile)
            {
                if (e.isOk)
                {
                    MessageBox.Show("文件已接收：" + (string)e.obj);
                }
                else
                {
                    MessageBox.Show("文件接收失败:" + (string)e.obj);
                }
            }
            else
            {
                if ((string)e.obj=="time")
                {
                    e.argReply = DateTime.Now;
                }
                else
                {
                    this.BeginInvoke(new Action(() =>
                    {
                        listBox2.Items.Add((string)e.obj);
                    }));
                }

            }

        }

        private void Server_clientOut(object sender, infoArg e)
        {
            voke(() => listBox1.Items.Add(e.endp+ "  离开"));
        }

        private void Server_clientIn(object sender, infoArg e)
        {
            voke(()=> { listBox1.Items.Add(e.endp + "  进入");
                label2.Text = (ct++).ToString();
            });
        }


        private void button5_Click(object sender, EventArgs e)
        {
            MessageBox.Show(server.getAllClients().Count.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex<0)
            {
                return;
            }
            var edp = listBox1.SelectedItem.ToString();
            var tpstr = edp.Substring(0, edp.IndexOf(' ')).Split(':');
            server.sendObject(new IPEndPoint(IPAddress.Parse(tpstr[0]),int.Parse(tpstr[1])), textBox2.Text,0);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0)
            {
                return;
            }
            var edp = listBox1.SelectedItem.ToString();
            var tpstr = edp.Substring(0, edp.IndexOf(' ')).Split(':');
            server.closeClient(new IPEndPoint(IPAddress.Parse(tpstr[0]), int.Parse(tpstr[1])));
            listBox1.Items.Add(edp.Substring(0, edp.IndexOf(' ')) + "  已断开");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            server.closeAllClients();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0)
            {
                return;
            }
            var edp = listBox1.SelectedItem.ToString();
            var tpstr = edp.Substring(0, edp.IndexOf(' ')).Split(':');
            OpenFileDialog xzwj = new OpenFileDialog();
            if (xzwj.ShowDialog() == DialogResult.OK)
            {

                FileStream cswj = new FileStream(xzwj.FileName, FileMode.Open, FileAccess.Read);
                byte[] tmpbf = new byte[cswj.Length];
                cswj.Read(tmpbf, 0, tmpbf.Length);
                cswj.Close();
                server.sendObject(new IPEndPoint(IPAddress.Parse(tpstr[0]), int.Parse(tpstr[1])), tmpbf,1);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0)
            {
                return;
            }
            var edp = listBox1.SelectedItem.ToString();
            var tpstr = edp.Substring(0, edp.IndexOf(' ')).Split(':');
            OpenFileDialog xzwj = new OpenFileDialog();
            if (xzwj.ShowDialog() == DialogResult.OK)
            {
                server.sendObject(new IPEndPoint(IPAddress.Parse(tpstr[0]), int.Parse(tpstr[1])), "sendfile|" + xzwj.FileName,0);
            }
        }


    }
}
